package com.aim.util;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

import com.aim.nada.GetTokenRequest;
import com.aim.nada.GetVehiclesByVinRequest;
import com.aim.nada.NadaConstants;
import com.aim.nada.VehicleBean;

public class NADAWebserviceUtil {
	public static String NADATOKEN = "";

	/**
	 * Get the token from NADA system
	 * 
	 * @return
	 */
	public static String getToken() {
		String nadaToken = "";
		SoapObject request = new SoapObject(NadaConstants.NADANAMESPACE,
				NadaConstants.GETTOKEN);
		GetTokenRequest requestParam = new GetTokenRequest();
		requestParam.Username = NadaConstants.USERNAME;
		requestParam.Password = NadaConstants.PASSWORD;
		request.addProperty(NadaConstants.TOKENREQUESTNAME, requestParam);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);
		envelope.dotNet = true;
		requestParam.register(envelope);
		HttpTransportSE httpTransportSE = new HttpTransportSE(
				NadaConstants.NADASECURITYURL);
		httpTransportSE
				.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		try {
			httpTransportSE.call(NadaConstants.GETTOKENSOAPACTION, envelope);
			nadaToken = envelope.getResponse().toString();
			NADATOKEN = nadaToken;
		} catch (Exception e) {
			e.printStackTrace();
			nadaToken = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><error>"
					+ e.getMessage() + "</error>";
			Log.e("NADA GET TOKEN", nadaToken);
		}
		return nadaToken;
	}

	/**
	 * 
	 * @param functionName
	 * @param token
	 * @return
	 */
	public static VehicleBean callNadaDetailWebservice(String vinNumber) {
		VehicleBean vehiclebean=new VehicleBean();
	
		if (NADATOKEN.length() <= 0) {
			getToken();
		}
		String returnXML = "";
		SoapObject request = new SoapObject(NadaConstants.NADANAMESPACE,
				NadaConstants.GETVEHICLES);
		GetVehiclesByVinRequest requestParam = new GetVehiclesByVinRequest();
		requestParam.Token = NADATOKEN;
		requestParam.Period = 0;
		requestParam.VehicleType = "UsedCar";
		requestParam.Vin = vinNumber;
		request.addProperty(NadaConstants.GETVEHICLESREQUEST, requestParam);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);
		envelope.dotNet = true;
		requestParam.register(envelope);
		HttpTransportSE httpTransportSE = new HttpTransportSE(
				NadaConstants.NADADETAILURL);
		httpTransportSE
				.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		try {
			httpTransportSE.call(NadaConstants.GETVEHICLESSOAPACTION, envelope);

			returnXML = envelope.getResponse().toString();	
			ArrayList vehicleList = getStringArrayInclude(returnXML,
					"Vehicle_Struc=anyType{", "};");
			if (vehicleList.size() > 0) {
				vehiclebean.setVinNumber(vinNumber);
				String contentForFirst = (String) vehicleList.get(0);
				ArrayList yearFirstList = getStringArrayInclude(
						contentForFirst, "Year=", ";");
				vehiclebean.setYear((String)yearFirstList.get(0));
				ArrayList makeFirstList = getStringArrayInclude(
						contentForFirst, "MakeDescr=", ";");				
				ArrayList modelFirstList = getStringArrayInclude(
						contentForFirst, "SeriesDescr=", ";");
				vehiclebean.setModel((String)modelFirstList.get(0));
				vehiclebean.setCarMake((String)makeFirstList.get(0));
			}

		} catch (Exception e) {
			Log.e("WebServiceUtil", "Error In Call Service", e);
			returnXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><error>"
					+ e.getMessage() + "</error>";
		}
		return vehiclebean;

	}

	public static ArrayList getStringArrayInclude(String content, String start,
			String end) throws IllegalArgumentException {

		ArrayList keys = new ArrayList();
		if (content != null) {
			StringBuffer contentBuffer = new StringBuffer(content);
			int tokenPos = contentBuffer.indexOf(start);
			while (tokenPos != -1) {
				contentBuffer.delete(0, tokenPos + start.length());
				tokenPos = contentBuffer.indexOf(end);
				keys.add(contentBuffer.substring(0, tokenPos));
				contentBuffer.delete(0, tokenPos + end.length());
				tokenPos = contentBuffer.indexOf(start);
			}
		}
		return keys;
	}

}
